package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.ShopSpecificationValue;
import com.addplus.townmall.server.api.model.town.expand.ShopSpecificationExpand;
import com.addplus.townmall.server.api.model.town.expand.ShopSpecificationNameValue;
import com.addplus.townmall.server.api.model.town.expand.ShopSpecificationValueExpand;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 商品规格值 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-08-27
 */
public interface ShopSpecificationValueMapper extends BaseAddMapper<ShopSpecificationValue> {

    @Select("SELECT\n" +
            " sn.id AS name_id,\n" +
            " sn.NAME AS NAME,\n" +
            " sv.id AS value_id,\n" +
            " sv.\n" +
            "VALUE\n" +
            "AS \n" +
            "VALUE\n" +
            " ,\n" +
            " sv.img AS image_path \n" +
            "FROM\n" +
            " shop_specification_value sv\n" +
            " LEFT JOIN shop_specification_name sn ON sv.specification_name_id = sn.id AND sn.is_deleted = 0 \n" +
            "WHERE\n" +
            " sv.shop_product_id = #{shopProductId}\n" +
            " AND sn.type = #{type} \n" +
            " AND sv.is_deleted = 0 \n" +
            "ORDER BY\n" +
            " sn.sort,\n" +
            " sv.sort")
    List<ShopSpecificationNameValue> getShopSpecificationNameValueList(@Param("shopProductId") Integer shopProductId, @Param("type") Integer type);


    @Select("SELECT sn.id as id ,sn.`name` as saleName FROM shop_specification_value sv LEFT JOIN shop_specification_name sn ON sv.specification_name_id = sn.id WHERE sv.shop_product_id = #{shopProductId} AND sv.is_deleted = 0 AND sn.is_deleted = 0 GROUP BY sn.id ORDER BY sn.sort")
    List<ShopSpecificationExpand> getSpecificationName(@Param("shopProductId") Integer shopProductId);

    @Select("SELECT n.`name` as name,v.`value` as value FROM shop_specification_name n,shop_specification_value v WHERE n.id=v.specification_name_id and v.id=#{id}")
    ShopSpecificationNameValue getShopSpecificationNameValue(@Param("id") Integer id);

    @Insert("<script>" +
            "insert into shop_specification_value(" +
            "specification_name_id," +
            "shop_product_id," +
            "value," +
            "is_img," +
            "img," +
            "sort," +
            "type," +
            "is_deleted," +
            "gmt_create," +
            "gmt_modified" +
            ") values" +
            "<foreach collection='list' item='item'  separator=','>" +
            "(" +
            "#{item.specificationNameId}," +
            "#{item.shopProductId}," +
            "#{item.value}," +
            "#{item.isImg}," +
            "#{item.img}," +
            "#{item.sort}," +
            "#{item.type}," +
            "#{item.isDeleted}," +
            "now()," +
            "now()" +
            ")" +
            "</foreach>" +
            "</script>")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void batchInsert(@Param("list") List<ShopSpecificationValueExpand> list);
}
